<style>
</style>
<template>
  <list_url ref="list_url" :config="config">
    <template #between_time="{row}">
      <p v-if="!row.start_time && !row.end_time">长期</p>
      <p class="text-ellipsis" v-if="row.start_time" :title="row.start_time">开始：{{row.start_time}}</p>
      <p class="text-ellipsis" v-if="row.end_time" :title="row.end_time">结束：{{row.end_time}}</p>
    </template>
    <template #action="{row}">
      <el-button @click="to_edit(row)" size="small">详情</el-button>
    </template>
  </list_url>
</template>
<script>
import {iframe, in_right_admin} from "@/js/tool.js";
import {ajax} from "@/js/api_tool.js";
import {_config, decode_config, param_encode, s94_event} from "@/js/s94_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');
    if(in_right_admin('UserTask/add')) btns['添加'] = function(){ iframe('user_task/edit') }
    if(in_right_admin('UserTask/delete')) btns['彻底删除'] = function(){ app.$refs['list_url'].delete() }

    let forms_config = {
      // create_time: {label:'创建时间',name:'where[create_time][between]',config:'Y-M-D',type:'date_between',value:''},
    };
    let list_cols = {
      id: {name:'id', label:'ID', width:'65px', fixed:'left', order:true},
      title: {name:'title', label:'任务标题', width:'120px'},
      info: {name:'info', label:'任务详情', type:'text_more', min_width:'120px'},
      condition_info: {name:'condition_info', label:'获得途径', type:'text_more', min_width: '120px'},

      frequency_info: {name:'frequency_info', label:'完成上限', width:'200px'},
      limit_score_level: {name:'limit_score_level', label:'段位限制', type:'checkbox', config:[], width:'80px'},
      level_list: {name:'level_list', label:'等级限制', type:'checkbox', config:[], width:'80px'},
      need_examine: {name:'need_examine', label:'需要审核', type:'radio_switch', width:'80px'},
      start_time: {name:'start_time', label:'开始时间', type:'date', width:'140px'},
      end_time: {name:'end_time', label:'结束时间', type:'date', width:'140px'},
      execute_title: {name:'execute_title', label:'任务完成后', width:'120px'},
    };

    let function_map = {};
    ajax({url:'/admin/user_task/config_map',cache:true,async:false},function (res){
      function_map = res.data.function_map;
      list_cols.limit_score_level.config = decode_config(res.data.score_level_list, {content_key:'title', value_key:'level'});
      list_cols.level_list.config = decode_config(res.data.level_list, {content_key:'name', value_key:'level'});
      return false;
    })


    if (is_del){
      list_cols['delete_time'] = {name:'delete_time', label:'删除时间', width:'165px', type:'datetime'};
    }else {
      list_cols['create_time'] = {name:'create_time', label:'创建时间', width:'165px'};
    }
    list_cols['action'] = {name:'action', label:'操作', width:'80px', type:'action', fixed:'right'};

    return {
      config: {
        api: '/admin/user_task/'+(is_del?'recycle':'list'),
        title: "用户任务列表"+(is_del?'(已删除)':''),
        btns: btns,
        forms_config: forms_config,
        list_cols: list_cols,
        resdata: function(list){
          let map = {all:'',day:'每日',month:'每月',year:'每年'}
          let key_map = {'user_frequency':'同一用户', 'link_frequency':'同一项目', 'user_link_frequency':'同一用户同一项目'};
          let frequency_info_arr = [];
          list.forEach(row=>{
            Object.keys(key_map).forEach(k=>{
              if (!row[k]) return;
              let arr = row[k].split(':');
              frequency_info_arr.push(`<p>${key_map[k]}${map[arr[0]]}最多完成${arr[1]}次</p>`);
            })
            row.frequency_info = frequency_info_arr.join('');
            row.need_examine = row.default_log_status === 0 ? 1 : 0;

            row.condition_info = app.condition_info(row);
          })
          return list;
        },
        limit: 20,
        p: this.$get('p', 1),
      },

      function_map: function_map,
    }
  },
  methods: {
    to_edit(row){
      let can_edit = in_right_admin('UserTask/edit') && ~[0,row.tenant_id].indexOf(_config('this_tenant.id'))
      let param = {
        id: row.id,
      }
      if (!can_edit) param['is_info'] = 1;
      iframe('user_task/edit?'+param_encode(param))
    },
    condition_info(condition){
      if (!this.function_map[condition.function]) return '';
      let fuc = this.function_map[condition.function];
      let param = JSON.parse(condition.param);
      let data = fuc.info_param || [];
      fuc.param.forEach(function(k,i){
        data[k] = param[i];
      })
      return vue_template(fuc.info, data);
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;

    // 监听修改和添加操作
    s94_event(['user_task/add','user_task/edit'], (evt)=>{
      let res = evt.data;
      if (res.data) this.$refs.list_url.list();
    })
  },
  unmounted() {
    // 监听修改和添加操作
    s94_event(['user_task/add','user_task/edit'], false)
  }
}
</script>
